<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 下午 3:57
 */
namespace app\auth\model;
use think\console\command\make\Model;
use think\Db;

class Auth extends Model{
    /**
     * 角色列表
     * @param $data
     * @return array
     */
    public function auth_list($data){
        #条件
        $where = 'del=1';
        $where.= isset($data['keyword'])? ' and CONCAT(auth_name) like "%'.$data['keyword'].'%"':'';
        $where.= isset($data['auth_pid'])?' and auth_pid='.$data['auth_pid']:'';
        $where.= isset($data['auth_level'])?' and auth_level='.$data['auth_level']:'';
        #总条数
        $total = Db::name('auth')->where($where)->count();
        #总页数
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }
        #列表
        $list = Db::name('auth')->where($where)->page($data['curr'],$data['limits'])->order(['sort'=>SORT_DESC])->select();

        #返回值
        $result = [
            'list'  =>$list,
            'page'  =>$page,
            'limits'=>$data['limits'],
            'total' =>$total,
            'curr'  =>$data['curr']
        ];
        return $result;
    }

    /**
     * 更改角色信息
     * @param $data
     * @return int|string
     */
    public function auth_update($data){
        if ($data['auth_pid']!=0){
            $path = Db::name('auth')->where(['auth_id'=>$data['auth_pid'],'del'=>1])->value('auth_path');
            $data['auth_path'] = $path.'-'.$data['auth_id'];
            $data['auth_level'] =count(explode('-',$data['auth_path']))-1;
        }

        $update = Db::name('auth')->where(['auth_id'=>$data['auth_id'],'del'=>1])->update($data);
        return $update;
    }

    /**
     * 添加角色
     * @param $data
     * @return int|string
     */
    public function auth_add($data){
        $add = Db::name('auth')->insertGetId($data);
        return $add;
    }

    /**
     * 删除角色
     * @param $data
     * @return int|string
     */
    public function auth_del($data){
        $del = Db::name('auth')->where(['auth_id'=>['in',$data['auth_id']],'del'=>1])->update(['del'=>0]);
        return $del;
    }


}